package com.ssy.lingxi.enhance.handler.async;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.enhance.entity.ProduceNoticeOrderDetailDO;
import com.ssy.lingxi.order.api.feign.OrderFeignService;
import com.ssy.lingxi.order.api.model.vo.request.OrderUpdateEnhanceFeignVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 异步服务 - 订单
 * @author fhj
 * @date 2020/12/23
 * @version 2.0.0
 */
@Slf4j
@Component
public class OrderAsyncService {
    @Resource
    private OrderFeignService orderFeignService;

    /**
     * 更新订单商品的加工数量
     * @param details 生产通知单明细集合
     * @param flag true为增加 false为扣减
     */
    @Async
    public void updateOrderProductProcessNum(List<ProduceNoticeOrderDetailDO> details, Boolean flag) {
        log.error("details参数为：{}", details.stream().map(ProduceNoticeOrderDetailDO::getId).collect(Collectors.toList()));
        try {
            List<OrderUpdateEnhanceFeignVO> feignList = details.stream().map(detail -> {
                OrderUpdateEnhanceFeignVO feign = new OrderUpdateEnhanceFeignVO();
                feign.setOrderProductId(detail.getOrderDetailId());
                feign.setEnhanceCount(flag ? BigDecimal.valueOf(detail.getProcessNum()) : BigDecimal.valueOf(-detail.getProcessNum()));
                return feign;
            }).collect(Collectors.toList());

            Wrapper<Void> result = orderFeignService.updateOrderProductEnhanceCount(feignList);
            if(ResponseCode.SUCCESS.getCode() != result.getCode()) {
                log.error("订单服务服务异常:" + result.getMessage());
            }
        } catch (Exception e) {
            log.error("调用订单服务修改订单商品详情接口失败：{}", e.getMessage());
        }
    }
}
